package net.dgg.rz.common.utils;

import net.dgg.rz.common.constant.ModuleCodeConstant;
import net.dgg.rz.common.excption.CommonExcption;
import net.dgg.rz.common.service.CommonMangerExtService;
import net.dgg.rz.production.dao.AssistantDao;
import net.dgg.rz.production.dao.ProductOrderDao;
import net.dgg.rz.production.entity.Assistant;
import net.dgg.rz.production.entity.ProductOrder;
import net.dgg.rz.production.exception.ProductOrderExcption;
import net.dgg.rz.production.service.ProductOrderService;
import net.dgg.tmd.foundation.platform.datapermission.entity.DataAuthDTO;
import net.dgg.tmd.foundation.platform.datapermission.service.DataPermissionAssocService;
import net.dgg.tmd.foundation.platform.session.SessionManager;
import net.dgg.tmd.foundation.platform.user.entity.UserEntity;
import org.apache.jasper.tagplugins.jstl.core.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @className DataPermissionUtils
 * @Author Quanlin Wan
 * @Description //TODO 数据权限参数注入
 * @Date 2018/12/26 10:33
 **/
@Component
public class DataPermissionUtils {

    @Autowired
    private DataPermissionAssocService dataPermissionAssocService;
    @Autowired
    private SessionManager sessionManager;
    @Autowired
    private CommonMangerExtService commonMangerExtService;
    @Autowired
    private AssistantDao assistantDao;//协单service
    @Autowired
    private ProductOrderDao productOrderDao;
    @Autowired
    private SearchUtils searchUtils;
    @Autowired
    ProductOrderService productOrderService;

    /**
     * @return
     * @Author Quanlin Wan
     * @Description //TODO 按流程人员id查询参数设置(待接收，已暂缓，生产明细数据)
     * @Date 10:35 2018/12/26
     * @Param
     **/
    public Map flowUserDataAccess(Map params,String code) {
        params.put("DATA_AUTH", "single");
        DataAuthDTO dataAuth = searchUtils.getcurrUserAuth(params, code);
        params.put("dataAuthDTO", dataAuth);
        params.put("userAlias", "t");
        params.put("userColumn", "flow_user_id");
        params.put("deptAlias", "t");
        params.put("deptColumn", "flow_user_org_id");
        return params;
    }
    /**
     * @Author Quanlin Wan
     * @Description //TODO
     * @Date 20:19 2019/5/22
     * @Param
     * @return
     **/


    public Map flowUserDataAccessReport(Map params,String code) {
        params.put("DATA_AUTH", "single");
        DataAuthDTO dataAuth = searchUtils.getcurrUserAuth(params, code);
        params.put("dataAuthDTO", dataAuth);
        params.put("userAlias", "po");
        params.put("userColumn", "flow_user_id");
        params.put("deptAlias", "po");
        params.put("deptColumn", "flow_user_org_id");
        return params;
    }

    /**
     * @Author Quanlin Wan
     * @Description //TODO 待接收（禁用）
     * @Date 15:39 2019/1/14
     * @Param
     * @return
     **/
    public Map waitAcceptDataAccess(Map params,String code) {
        params.put("DATA_AUTH", "single");
        DataAuthDTO dataAuth = searchUtils.getcurrUserAuth(params, code);
        //待接收人员权限
        params.put("dataAuthDTO", dataAuth);
        params.put("userAlias", "t");
        params.put("userColumn", "waiting_receiver_id");
        params.put("deptAlias", "t");
        params.put("deptColumn", "waiting_receiver_org_id");
        return params;
    }

    /**
     * @return
     * @Author Quanlin Wan
     * @Description //TODO 办理中页面权限参数
     * @Date 10:35 2018/12/26
     * @Param
     **/
    public Map inProcessDataAccess(Map params,String code) {
        List<Map> dataAuthList = new ArrayList<>();
        params.put("DATA_AUTH", "mulcon");
        DataAuthDTO dataAuth = searchUtils.getcurrUserAuth(params, code);
        //流程人员权限
        Map<String, Object> flowUser = new HashMap<>();
        flowUser.put("dataAuthDTO", dataAuth);
        flowUser.put("userAlias", "t");
        flowUser.put("userColumn", "flow_user_id");
        flowUser.put("deptAlias", "t");
        flowUser.put("deptColumn", "flow_user_org_id");

        //协单人员权限
        Map<String, Object> assistUser = new HashMap<>();
        assistUser.put("dataAuthDTO", dataAuth);
        assistUser.put("userAlias", "a");
        assistUser.put("userColumn", "assist_user_id");
        assistUser.put("deptAlias", "a");
        assistUser.put("deptColumn", "assist_user_org_id");
        if (StringUtils.isNotEmpty(params.get("assist"))) {
            switch (params.get("assist").toString()) {
                //默认协单和非协单都显示
           /*     case "0":
                    dataAuthList.add(flowUser);
                    dataAuthList.add(assistUser);
                    break;*/
                //不看协单
                case "1":
                    dataAuthList.add(flowUser);
                    break;
                //只看协单
                case "2":
                    dataAuthList.add(assistUser);
                    break;
                default:
                    dataAuthList.add(flowUser);
                    dataAuthList.add(assistUser);
                    break;
            }
        } else {
            dataAuthList.add(flowUser);
            dataAuthList.add(assistUser);
        }
        params.put("dataAuthList", dataAuthList);
        return params;
    }


    /**
     * @Author Quanlin Wan
     * @Description //TODO 按审核人员id查询参数设置(待审核页面,数据单独使用了一条sql)
     * @Date 11:34 2018/12/26
     * @Param 
     * @return 
     **/
    public Map auditorDataAccess(Map params) {
        params.put("DATA_AUTH", "mulcon");
        List<Map> dataAuthList = new ArrayList<>();
        DataAuthDTO dataAuth = searchUtils.getcurrUserAuth(params, ModuleCodeConstant.RZSC_AUDITORDER_CODE);
        Map<String, Object> auditMap = new HashMap<>();

        //审核人员权限
        auditMap.put("dataAuthDTO", dataAuth);
        auditMap.put("userAlias", "t");
        auditMap.put("userColumn", "auditor_id");
        dataAuthList.add(auditMap);
        //流程人员权限
        Map<String, Object> flowUser = new HashMap<>();
        flowUser.put("dataAuthDTO", dataAuth);
        flowUser.put("userAlias", "t");
        flowUser.put("userColumn", "flow_user_id");
        flowUser.put("deptAlias", "t");
        flowUser.put("deptColumn", "flow_user_org_id");
        dataAuthList.add(flowUser);
        //生产所属权限
//        Map<String, Object> productOrg = new HashMap<>();
        UserEntity userEntity = searchUtils.getcurrUser(params);
        ValidateUtils.strNotEmpty(userEntity, CommonExcption.class, "未登录，请登录后再试");

        Map busDep = commonMangerExtService.findBusDeptById(userEntity.getOrgId());
        //生产所属权限
        DataAuthDTO proDataAuth = new DataAuthDTO();
        List<Long> orgIds = new ArrayList<>();
        orgIds.addAll(dataAuth.getOrgIds());
        //如果当前登录人组织架构在事业部之下的部门，需加入其所属事业部id
        if (StringUtils.isNotEmpty(busDep) && !orgIds.contains(busDep.get("id"))) {
            orgIds.add(Long.valueOf(busDep.get("id").toString()));
        }
        proDataAuth.setIsAll(dataAuth.getIsAll());
        proDataAuth.setOrgIds(orgIds);
        proDataAuth.setUserId(dataAuth.getUserId());
        params.put("dataAuthDTO", proDataAuth);
        params.put("deptAlias", "t");
        params.put("deptColumn", "product_org_id");
        //人员权限
        params.put("dataAuthList", dataAuthList);
        return params;
    }
    /**
     * @Author Quanlin Wan
     * @Description //TODO 生产所属权限（待分配页面、全部订单）
     * @Date 9:37 2018/12/27
     * @Param
     * @return
     **/
    public Map productOrgDataAccess(Map params,String code) {
        params.put("DATA_AUTH", "single");
        DataAuthDTO dataAuth = searchUtils.getcurrUserAuth(params,code);
//        DataAuthDTO dataAuth = dataPermissionAssocService.selectAuthToConfigureRturnOrgIds(code);
            //将部门权限更改为事业部
            UserEntity userEntity = searchUtils.getcurrUser(params);
            ValidateUtils.strNotEmpty(userEntity, CommonExcption.class, "未登录，请登录后再试");
            Map busDep = commonMangerExtService.findBusDeptById(userEntity.getOrgId());
        if (ValidateUtils.strNotEmpty(busDep, null, null) && !dataAuth.getOrgIds().contains(busDep.get("id"))) {
            List<Long> orgIds = dataAuth.getOrgIds();
            orgIds.add(Long.valueOf(busDep.get("id").toString()));
            dataAuth.setOrgIds(orgIds);
        }
        params.put("dataAuthDTO", dataAuth);
        params.put("deptAlias", "t");
        params.put("deptColumn", "product_org_id");
        return params;
    }
    /**
     * @Author Quanlin Wan
     * @Description //TODO （已分配记录页面）
     * @Date 9:37 2018/12/27
     * @Param
     * @return
     **/
    public Map allotRecordDataAccess(Map params) {
        params.put("DATA_AUTH", "single");
        DataAuthDTO dataAuth = searchUtils.getcurrUserAuth(params, ModuleCodeConstant.RZSC_ALLOTRECORD_CODE);
//        DataAuthDTO dataAuth = dataPermissionAssocService.selectAuthToConfigureRturnOrgIds(ModuleCodeConstant.RZSC_ALLOTRECORD_CODE);
        params.put("dataAuthDTO", dataAuth);
        params.put("userAlias", "t");
        params.put("userColumn", "allot_user_id");
        params.put("deptAlias", "t");
        params.put("deptColumn", "allot_user_org_id");
        return params;
    }


    /**
     * @Author Quanlin Wan
     * @Description //TODO 上传文件操作权限验证
     * @Date 11:33 2019/1/21
     * @Param scProductOrderId 生产订单Id  currUserId(PC端不传）
     * @return
     **/
    public ProductOrder uoloadFileDataAccess(String scProductOrderId,String userLoginName) {
        HashMap<String, Long> map = new HashMap<>();
        map.put("scProductOrderId", Long.valueOf(scProductOrderId));
        List<Assistant> assistantList = assistantDao.query(map);
        ProductOrder productOrder = productOrderService.selectByPrimaryKey(Long.valueOf(scProductOrderId));
        ValidateUtils.strNotEmpty(productOrder, ProductOrderExcption.class, "未查询到对应生产订单");
        List<Long> userIds = new ArrayList<>();
        if (assistantList.size() > 0) {
            for (Assistant assistant : assistantList) {
                userIds.add(assistant.getAssistUserId());
            }
        }
        userIds.add(productOrder.getFlowUserId());
        UserEntity userEntity = searchUtils.getcurrUser(new HashMap(){{
            put("userLoginName", userLoginName);
        }});
        ValidateUtils.strNotEmpty(userEntity, ProductOrderExcption.class, "当前未登录，请登录后再进行操作");
        ValidateUtils.isTrue(userIds.contains(userEntity.getId()), ProductOrderExcption.class, "不是当前订单的流程人员/不能上传文件");
        return productOrder;
    }

    /**
     * @return
     * @Author zyou
     * @Description 部门提成数据权限
     * @Param
     **/
    public Map deptRoyaltyDataAccess(Map params,String code) {
        params.put("DATA_AUTH", "single");
        DataAuthDTO dataAuth = searchUtils.getcurrUserAuth(params, code);
        params.put("dataAuthDTO", dataAuth);
        params.put("userAlias", "ui");
        params.put("userColumn", "id");
        params.put("deptAlias", "ui");
        params.put("deptColumn", "org_id");
        return params;
    }
}
